package cn.lls.tps;

import cn.lls.base.exception.LlsException;
import cn.lls.base.result.ResultCode;
import cn.lls.base.util.EmailUtil;
import cn.lls.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 引用邮件工具
 * 采取线程池异步发送邮件
 *
 * @ClassName EmailTPS
 * @Author 划水艺术家
 * @Date 2022/11/20 23:01
 * @Version 1.0
 **/
@Component
public class EmailTPS {
    @Autowired
    private EmailService emailService;

    // 异步发送邮箱验证码
    @Async("llsTask")
    public void sendCode(String toEmail){
        try{
            // 异步执行
            Thread.sleep(1000);
            emailService.sendCode(EmailUtil.creatCode(), toEmail);
        } catch (Exception e){
            // 抛出验证码发送失败异常
            throw new LlsException(ResultCode.CODE_SEND_FAIL);
        }
    }

    // 异步发送公司审核通过的邮箱
    @Async("llsTask")
    public void sendAgreeMessage(String companyName, String account, String loginPassword, String toEmail) {
        try{
            // 异步执行
            Thread.sleep(1000);
            emailService.sendAgreeMessage(companyName, account, loginPassword, toEmail);
        } catch (Exception e){
            // 抛出验证码发送失败异常
            throw new LlsException(ResultCode.EMAIL_SEND_FAIL);
        }
    }

    // 异步发送公司审核未通过的邮箱
    @Async("llsTask")
    public void sendRefuseMessage(String companyName, String message, String toEmail) {
        try{
            // 异步执行
            Thread.sleep(1000);
            emailService.sendRefuseMessage(companyName, message, toEmail);
        } catch (Exception e){
            throw new LlsException(ResultCode.EMAIL_SEND_FAIL);
        }
    }

    // 异步发送公司被封禁的邮箱
    @Async("llsTask")
    public void sendBannedMessage(String companyName, String message, String toEmail) {
        try{
            // 异步执行
            Thread.sleep(1000);
            emailService.sendBannedMessage(companyName, message, toEmail);
        } catch (Exception e){
            throw new LlsException(ResultCode.EMAIL_SEND_FAIL);
        }
    }

    // 异步发送公司解封的邮箱
    @Async("llsTask")
    public void sendUnblockMessage(String companyName, String toEmail) {
        try{
            // 异步执行
            Thread.sleep(1000);
            emailService.sendUnblockMessage(companyName, toEmail);
        } catch (Exception e){
            throw new LlsException(ResultCode.EMAIL_SEND_FAIL);
        }
    }

}
